package com.ffcs.ffxtc.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ffcs.domin.UserInfo;
import com.ffcs.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangzhengxing
 */
@Configuration
@Slf4j
public class MybatisMetaObjectHandler implements MetaObjectHandler {
    /**
     * 创建时增加的字段内容
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        UserInfo userInfo = UserUtils.getUserInfo();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setFieldValByName("createdTime", formatter.format(new Date()), metaObject);
        this.setFieldValByName("createdBy", userInfo.getRealname(), metaObject);
        this.setFieldValByName("deleted", 1, metaObject);
    }

    /**
     * 修改时候增加的字段内容
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        UserInfo userInfo = UserUtils.getUserInfo();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setFieldValByName("updatedTime", formatter.format(new Date()), metaObject);
        this.setFieldValByName("updatedBy", userInfo.getRealname(), metaObject);
    }
}